var ws,store_name,avatar,key,store_id='';
var app = angular.module('store',[]);
app.controller('storeController',function($scope,$http,$filter){
	$scope.initView = function(user_pic,pics){
		$scope.avatar = avatar;
		$scope.store_name = store_name;
		$scope.user_pic = user_pic; 
//		console.log(apiRoot + "?act=seller_order&op=order_list");
		plus.nativeUI.showWaiting('加载中...');
		$http({
			method:"post",
			url:apiRoot + "?act=seller_order&op=order_list",
			data:{
				key:key,
				state_type:'store_order',
				store_flag:'mobile'
			},
			headers:{'Content-Type': 'application/x-www-form-urlencoded'},
	      	transformRequest: function (data) {
                    return $.param(data);
             }
		}).then(function SuccessCallback(data){ 
//			console.log(JSON.stringify(data));
			$scope.order_list = data.data.datas.order_list; 
			$scope.pics = pics;
			setTimeout(function(){
				plus.nativeUI.closeWaiting();
			},2000)
//			$scope.a = $filter('filter')(data.data.datas.order_list,data.data.datas.order_list.order_state,30);
//			console.log(JSON.stringify($scope.a));
						
		},function errorfunction(e){
			plus.nativeUI.closeWaiting();
			error();
		}) 
	}
	$scope.iteams = function (order_sn) {
		var nwaiting = plus.nativeUI.showWaiting();//显示原生等待框
		var webviewContent= plus.webview.create('./orders_goods.html','orders_goods.html',{},{order_sn:order_sn,spage:ws.id});//后台创建webview并打开show.html
		webviewContent.addEventListener("loaded", function() { //注册新webview的载入完成事件
	        nwaiting.close(); //新webview的载入完毕后关闭等待框
	        webviewContent.show("slide-in-right",500); //把新webview窗体显示出来，显示动画效果为速度200毫秒的右侧移入动画
	    }, false);
	}
	$scope.cancel = function (order_id) {      //取消订单
		plus.nativeUI.showWaiting('进行中...');
		$http({
			method:"post",
			url:apiRoot + "?act=seller_order&op=order_cancel",
			data:{
				key:key,
				order_id:order_id,
			},
			headers:{'Content-Type': 'application/x-www-form-urlencoded'},
	      	transformRequest: function (data) {
                    return $.param(data);
             }
		}).then(function SuccessCallback(info){ 
			console.log(JSON.stringify(info.data.code));
			plus.nativeUI.closeWaiting();
			if(info.data.code == 200){
				toast('取消成功！');
				$scope.initView(user_pic,pics);
			}else{
				toast(info.data.datas.error);
			}
						
		},function errorfunction(e){
			plus.nativeUI.closeWaiting();
			error();
		}) 
	}
	$scope.distribution = function (order_id) {      //配送
		plus.nativeUI.showWaiting('进行中...');
		$http({
			method:"post",
			url:apiRoot + "?act=seller_order&op=order_deliver_send",
			data:{
				key:key,
				order_id:order_id,
			},
			headers:{'Content-Type': 'application/x-www-form-urlencoded'},
	      	transformRequest: function (data) {
                    return $.param(data);
             }
		}).then(function SuccessCallback(info){ 
			console.log(JSON.stringify(info.data.code));
			plus.nativeUI.closeWaiting();
			if(info.data.code == 200){
				toast('配送成功！');
				$scope.initView(user_pic,pics);
			}else{
				toast(info.data.datas.error);
			}
						
		},function errorfunction(e){
			plus.nativeUI.closeWaiting();
			error();
		}) 
	} 
	$scope.complete = function (order_id) {      //完成订单
		plus.nativeUI.showWaiting('进行中...');
		$http({
			method:"post",
			url:apiRoot + "?act=seller_order&op=order_complete",
			data:{
				key:key,
				order_id:order_id,
			},
			headers:{'Content-Type': 'application/x-www-form-urlencoded'},
	      	transformRequest: function (data) {
                    return $.param(data);
             }
		}).then(function SuccessCallback(info){ 
			console.log(JSON.stringify(info.data.code));
			plus.nativeUI.closeWaiting();
			if(info.data.code == 200){
				toast('操作成功！');
				$scope.initView(user_pic,pics);
			}else{
				toast(info.data.datas.error);
			}
						
		},function errorfunction(e){
			plus.nativeUI.closeWaiting();
			error();
		}) 
	}
}) 
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	store_id = plus.storage.getItem('storeid');
	store_name = plus.storage.getItem('store_name');
	avatar = plus.storage.getItem('avatar');
	key = plus.storage.getItem('key');
	console.log(key+'--'+store_id);
	aboutElement = document.querySelector('[ng-controller=storeController]');
	$scope = angular.element(aboutElement).scope();
	$scope.initView(user_pic,pics);
	$scope.$apply();
	
	
		//个人资料
	$(".Personl").on("tap",function(){
		var nwaiting = plus.nativeUI.showWaiting();//显示原生等待框
		var webviewContent= plus.webview.create('./Personal_data.html','Personal_data.html',{},{spage:ws.id});//后台创建webview并打开show.html
		webviewContent.addEventListener("loaded", function() { //注册新webview的载入完成事件
	        nwaiting.close(); //新webview的载入完毕后关闭等待框
	        webviewContent.show("slide-in-right",500); //把新webview窗体显示出来，显示动画效果为速度200毫秒的右侧移入动画
	    }, false);
//		openNewPage('Personal_data.html')
	})
	
},false)
